function isValidtel(telno){
    var str = telno.replace(/^\s*|\s*$/g, "");
    var first2Char = str.charAt(0) + str.charAt(1);
    var cntSpace = 0;
    if (str.length == 0) {
        return true;
    }
    if (first2Char !== "01" && first2Char != "02" && first2Char != "07") {
        return false;
    }
    for (i = 0; i < str.length; i++) {
        if (str.charAt(i) != '0' && str.charAt(i) != '1' &&
        str.charAt(i) != '2' &&
        str.charAt(i) != '2' &&
        str.charAt(i) != '3' &&
        str.charAt(i) != '4' &&
        str.charAt(i) != '5' &&
        str.charAt(i) != '6' &&
        str.charAt(i) != '7' &&
        str.charAt(i) != '8' &&
        str.charAt(i) != '9' &&
        str.charAt(i) != ' ') {
            return false;
        }
        if (str.charAt(i) == ' ') {
            cntSpace++;
        }
    }
    if (str.length - cntSpace != 11) {
        return false;
    }
    return true;
}


function validEmail(email){
    var supported = 0;
    var localPartAllowedChars = "[A-Za-z0-9\\.\\-_'/]";
    var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)|(\\.@)");
    var r2 = new RegExp("^" + localPartAllowedChars + "+\\@[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})$");
    return (!r1.test(email) && r2.test(email));
} 

function isValidName(N){
    bOk = true;
    sTmp = N.toUpperCase();
    cTmpPrev = "X";
    if (sTmp.length == 0) {
        bOk = false;
    }
    for (iTmp = 0; iTmp < sTmp.length; iTmp++) {
        cTmp = sTmp.charAt(iTmp);
        if (cTmp < "A" || cTmp > "Z") {
            bOk = false;
        }
        if (cTmp == " " || cTmp == "-" || cTmp == "'") {
            bOk = true;
        }
        if (cTmp == "-" && cTmpPrev == "-") {
            bOk = false;
            cTmpPrev = cTmp;
        }
    }
    return bOk;
} 

function validateAddEmployee(){
	
    var error = "";
    var title = document.mainform.e_title.value;
    if (title == "") {
        error += "&nbsp;&bull; Please select a title.<br/>";
    }
    fname = document.mainform.e_firstname.value;
    if (fname == "") {
        error += "&nbsp;&bull; A first name must be entered.<br/>";
    }
    else 
        if (fname.length < 2) {
            error += "&nbsp;&bull;First name must be at least two characters in length.<br/>";
        }
        else 
            if (fname.length > 20) {
                error += "&nbsp;&bull; First name must be no more than 20 characters in length.<br/>";
            }
            else 
                if (!isValidName(fname)) {
                    error += "&nbsp;&bull; First name may contain alphabetical characters, spaces and the following special characters only (' or -).<br/>";
                }
    sname = document.mainform.e_surname.value;
    if (sname == "") {
        error += "&nbsp;&bull; A last name must be entered.<br/>";
    }
    else 
        if (sname.length < 2) {
            error += "&nbsp;&bull; Last name must be at least two characters in length.<br/>";
        }
        else 
            if (sname.length > 40) {
                error += "&nbsp;&bull; Last name must be no more than 40 characters in length.<br/>";
            }
            else 
                if (!isValidName(sname)) {
                    error += "&nbsp;&bull; Last name may contain alphabetical characters, spaces and the following special characters only (' or -).<br/>";
                }
    tel = document.mainform.e_phone_no.value;

    if (tel == "") {
        error += "&nbsp;&bull; You must enter either a home contact or mobile contact.<br/>";
    }
    else {
        
            if (!isValidtel(tel)) {
                error += "&nbsp;&bull; Phone number given is not a valid phone number.<br/>";
            }
            else 
                if (tel.length != 11) {
                    error += "&nbsp;&bull;Phone number should have 11 digits .<br/>";
                }
                else 
                    if ((tel.substring(0, 2) != "01") && 
						(tel.substring(0, 2) != "07") &&
						(tel.substring(0, 2) != "02")) {
                        error += "&nbsp;&bull; Phone number should start with 01, 02 or 07.<br/>";
                    }
        
    }

    var mail = trim(document.mainform.e_email.value);
    if (mail == "") {
        error += "&nbsp;&bull; An email address must be entered.<br/>";
    }
    else 
        if (mail.length > 30) {
            error += "&nbsp;&bull; Email address must be no more than 30 characters in length.<br/>";
        }
        else 
            if (!validEmail(mail)) {
                error += "&nbsp;&bull; Enter a valid email address.<br/>";
            }
    mailRepeat = trim(document.mainform.e_repeat_email.value);
    if (mailRepeat == "") {
        error += "&nbsp;&bull; Email address must be re-entered.<br/>";
    }
    else 
        if (mail != mailRepeat) {
            error += "&nbsp;&bull; The repeated email address does not match the previous e-mail address entered.<br/>";
        }
   var hours = document.mainform.e_hours_per_day.value;
    if (hours == "") {
        error += "&nbsp;&bull; Please select hours.<br/>";
    }
	    
    if (error != "") {
        var browser = navigator.appName;
        if (browser == "Microsoft Internet Explorer") {
             document.all["errorMessage"].innerHTML = error;
        }
        else 
            if (browser == "Netscape") {
                document.getElementById("errorMessage").innerHTML = error;
            }     
		return false;  
    }
    else {
        return true;
    }
} 